package com.damage.common.util;

import jodd.util.StringUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.beanutils.ConvertUtils;

import java.util.HashMap;
import java.util.Map;

@Data
public class Variable {

    private String keys;
    private String values;
    private String types;

    public Map<String, Object> getVariableMap() {
        Map<String, Object> vars = new HashMap<String, Object>();

        //注册日期-字符串类型转换器
        ConvertUtils.register(new DateConverter(), java.util.Date.class);

        //判断键是否为null或者空格
        if (StringUtil.isBlank(keys)) {
            return vars;
        }

        //键，值，类型 是以','分割的
        String[] arrayKey = keys.split(",");
        String[] arrayValue = values.split(",");
        String[] arrayType = types.split(",");
        for (int i = 0; i < arrayKey.length; i++) {
            if ("".equals(arrayKey[i]) || "".equals(arrayValue[i]) || "".equals(arrayType[i])) {
                continue;
            }
            String key = arrayKey[i];
            String value = arrayValue[i];
            String type = arrayType[i];

            Class<?> targetType = Enum.valueOf(PropertyType.class, type).getValue();
            Object objectValue = ConvertUtils.convert(value, targetType);
            vars.put(key, objectValue);
        }
        return vars;
    }
}
